/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace _videoConference.DAL
{
    /// <summary>
    /// Summary description for Exception
    /// </summary>
    public static class clsException
    {
        public static void LogError(Exception ex)
        {
            try
            {
                System.Threading.Thread thrdError = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(PunchError));
                thrdError.Priority = System.Threading.ThreadPriority.Lowest;
                thrdError.IsBackground = true;
                thrdError.Start(ex);
                thrdError.Join();
            }
            catch (Exception exp)
            {
                System.Windows.MessageBox.Show("Video--:--clsException.cs--:--LogError()--:--" + exp.Message);
            }
        }

        private static void PunchError(object objException)
        {
            try
            {
                WriteToDatabase((Exception)objException);
                WriteToErrorLogFile((Exception)objException);
                WriteToEventLog((Exception)objException);
            }
            catch (Exception exp)
            {
                System.Windows.MessageBox.Show("Video--:--clsException.cs--:--PunchError()--:--" + exp.Message);
            }
        }

        private static void WriteToDatabase(Exception ex)
        {
            try
            {
                SqlParameter ParameterMsg = new SqlParameter("@Msg", System.Data.SqlDbType.VarChar);
                if (ex.Message != null)
                {
                    ParameterMsg.Value = ex.Message;
                }
                else
                {
                    ParameterMsg.Value = "Nill";
                }

                SqlParameter ParameterStackTrace = new SqlParameter("@StackTrace", System.Data.SqlDbType.VarChar);
                if (ex.StackTrace != null)
                {
                    ParameterStackTrace.Value = ex.StackTrace;
                }
                else
                {
                    ParameterStackTrace.Value = "Nill";
                }

                SqlParameter ParameterAdditionalInfo = new SqlParameter("@AdditionalInfo", System.Data.SqlDbType.VarChar);
                if (ex.InnerException != null && ex.InnerException.Message != null)
                {
                    ParameterAdditionalInfo.Value = ex.InnerException.Message;
                }
                else
                {
                    ParameterAdditionalInfo.Value = "Nill";
                }


                SqlParameter ParameterSource = new SqlParameter("@Source", System.Data.SqlDbType.VarChar);
                if (ex.Source != null)
                {
                    ParameterSource.Value = ex.Source;
                }
                else
                {
                    ParameterSource.Value = "Nill";
                }

                SqlParameter ParameterLocation = new SqlParameter("@Location", System.Data.SqlDbType.VarChar);
                if (ex.Data != null && ex.Data["My Key"] != null)
                {
                    ParameterLocation.Value = ex.Data["My Key"].ToString();
                }
                else
                {
                    ParameterLocation.Value = "Nill";
                }

                SqlParameter ParameterDate = new SqlParameter("@Date", System.Data.SqlDbType.DateTime);
                ParameterDate.Value = DateTime.Now;

                SqlHelper.ExecuteNonQuery(Database.conStr, CommandType.StoredProcedure, "sp_Exception_Add", ParameterStackTrace, ParameterAdditionalInfo, ParameterMsg, ParameterSource, ParameterLocation, ParameterDate);
            }
            catch (Exception exp)
            {
                WriteToErrorLogFile(exp);
                WriteToEventLog(exp);
            }
        }

        private static void WriteToErrorLogFile(Exception ex)
        {
            try
            {
                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                while (fs.ReadByte() != -1)
                { }
                sw.Write("Title: Error" + sw.NewLine);
                sw.Write("Error Message: " + ex.Message + sw.NewLine);
                sw.Write("StackTrace is as follow: " + sw.NewLine + ex.StackTrace + sw.NewLine);
                sw.Write("Date - Time: " + DateTime.Now.ToString() + sw.NewLine);
                sw.Write("===========================================================================================");
                sw.Close();
                fs.Close();
            }
            catch (Exception exp)
            {
                System.Windows.MessageBox.Show("Video--:--clsException.cs--:--WriteToErrorLogFile()--:--" + exp.Message);
            }
        }

        private static void WriteToEventLog(Exception ex)
        {
            try
            {
                EventLog objEventLog = new EventLog();
                if (!(EventLog.SourceExists("1videoConference")))
                {
                    EventLog.CreateEventSource("1videoConference", "1videoConference");
                }
                objEventLog.Source = "1videoConference";
                objEventLog.WriteEntry("Error Message: " + ex.Message + "\r\n\nStackTrace is as follow:" + Char.ConvertFromUtf32(13) + ex.StackTrace, EventLogEntryType.Error);
            }
            catch (Exception exp)
            {
                WriteToErrorLogFile(exp);
            }
        }
    }
}